#!/usr/bin/python
# Copyright 2011, Kitware, Inc.

import sys

if len(sys.argv) == 2:
    filename = sys.argv[1]
else:
    filename = '/home/tutorial/Tutorial/Data/cthead1.png'

import SimpleITK as sitk
import numpy as np
import pylab

image = sitk.ReadImage(filename)

a = sitk.GetArrayFromImage(image)

h, bins = np.histogram(a, bins=255, range=(0, 255))

mode = 0
for i in range(1, h.shape[0]):
    if h[i] > h[mode]:
        mode = i
print "Mode of image: " + str(mode)

# Print histogram
pylab.figure()
pylab.hist(a.flatten())
pylab.show()
